package com.ncloud.crm.utils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by shiqiang on 2017/4/14.
 */
public class ItemInfoUtils {
    public static JSONArray getProjectAttr(JSONObject attrGroup,JSONObject target,String type){
        JSONArray result = new JSONArray();
        if(attrGroup==null){
            return result;
        }
        for (Map.Entry<String, Object> entry : attrGroup.entrySet()) {
            JSONObject jsonObject = new JSONObject();
            jsonObject.putAll(target);
            if(type != null ){
                jsonObject.put("attrType", type);
            }
            String key = entry.getKey();
            if(!"modifyTag".equals(key)){
                jsonObject.put("attrCode", key);
                jsonObject.put("attrValue", entry.getValue());
            }
            result.add(jsonObject);
        }
        return result;
    }

    /*对属性值是金额的code进行处理,*/
    public static Long getMultiMoney(Object money,int precision){
        String value = String.valueOf(money);
        BigDecimal bigDecimal = new BigDecimal(StringUtils.isEmpty(value)?"0":value);
        return bigDecimal.multiply(BigDecimal.valueOf(precision)).longValue();
    }
    /*对属性值是金额的code进行处理,*/
    public static Double getDivideMoney(Object money,int precision){
        BigDecimal bigDecimal = new BigDecimal(String.valueOf(money));
       return bigDecimal.divide(BigDecimal.valueOf(precision)).doubleValue();
    }
    /*对金额进行特殊处理*/
    public static boolean isMoney(String code){
        boolean flag = false;
        List<String> keys = new ArrayList<String>();
        /*receiveFee:receivedFee,payFee:payFee,balance:balance,standard:standard,billingCycle:billingCycle,cycleUnit:cycleUnit*/
        keys.add("receiveFee");
        keys.add("receivedFee");
        keys.add("payFee");
        keys.add("balance");
        keys.add("standard");
        if(keys.contains(code)){
            flag = true;
        }
        return flag;
    }
}
